function [y,signal] = DSkuopin(LN,L,SNR,W)
% 直接序列扩频BPSK调制系统
% 返回值y为误码率，signal为发送信号


data = randi([0,1], 1, LN);     %  产生随机数作为发送数据 % data = randint(1,LN,[0,1]);
data_bipolar = 1 - 2*data;      %  将数据转为双极性序列，0对应于1，1对应于-1
PN = mserial_generator([1 1 0 0 0 0 1 1 1]);  %产生PN码
LPN = length(PN);               %  "计算PN码的长度，此处即为扩频比
PN = ones(1,LPN) - 2 * PN;      %  将PN码转为双极性序列
PN2 = fliplr(PN);               %  完成PN码反转
data_upsample_pn = upsample(data_bipolar,LPN);
DS_data = conv(data_upsample_pn, PN);   % 完成直扩
DS_data_upsample = upsample(DS_data,L);
rcos_fir = rcosfir(0.6,6,L,1,'sqrt');         % 产生根升余弦滤波器
rconv = conv(DS_data_upsample,rcos_fir);      % 完成根升余弦成型
carrier =cos(2*pi*W* (1:length(rconv)) );     % 产生载波
Rsend = rconv.*carrier;                       % 完成BPSK调制
Rrecv = awgn(Rsend, SNR, 'measured');         % 通过信道，加噪
data_rec = Rrecv.*carrier;                    % 将接收信号与载波相乘
data_rconv = conv(data_rec,rcos_fir);         % 信号通过接收匹配滤波器
data_rDS = conv(data_rconv,upsample(PN2,L));  % 信号与本地PN码相关
rdata_biplolar = downsample(data_rDS,L*LPN,88);% 采样
rdata(rdata_biplolar<0)=1;
rdata(rdata_biplolar>0)=0;%判决
Rdata = rdata(2:LN+1);

c=0;                  % 误码率计算
for n=1:LN
    if(data(n) ~= Rdata(n))  
        c=c+1;
    end 
end
y=c/LN;
signal = Rsend;